package cn.ytr.crm.service.impl;

import cn.ytr.crm.domain.Customer;
import cn.ytr.crm.domain.Employee;
import cn.ytr.crm.mapper.CustomerMapper;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.ICustomerService;
import cn.ytr.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService{

	@Autowired
	private CustomerMapper customerMapper;

	/**
	 * 增加或保存客户
	 */
	@Override
	public void saveOrUpate(Customer entity) {
		if (entity.getId() != null) {
			customerMapper.updateByPrimaryKey(entity);
		} else {
            //如果是新增客户，要设置录入人、销售人为当前登录的用户
            Employee employee = UserContext.getCurrentUserByShiro();
            entity.setInputUser(employee);
            entity.setSeller(employee);
			customerMapper.insert(entity);
		}
	}

	/**
	 * 删除客户
	 */
	@Override
	public void delete(Long id) {
		customerMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取单个客户
	 */
	@Override
	@Transactional(readOnly=true)
	public Customer get(Long id) {
		Customer customer = customerMapper.selectByPrimaryKey(id);
		return customer;
	}

	/**
	 * 获取所有客户
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Customer> listAll() {
		List<Customer> list = customerMapper.selectAll();
		return list;
	}

	/**
	 * 高级查询所有客户
	 */
	@Override
	@Transactional(readOnly=true)
	public PageInfo query(QueryObject qo) {
		/*Integer count = customerMapper.selectForCount(qo);
		if (count < 1 || count == null) {
			return new PageResult(qo.getPageSize());
		} else {
			List<Customer> list = customerMapper.selectForList(qo);
			return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
		}*/

		//改用分页插件 pageHelper
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<Customer> list = customerMapper.selectForList(qo);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}

	/**
	 * 修改客户状态
	 */
	public void updateStatus(Long customerId, Long status) {
        customerMapper.updateStatus(customerId, status);
	}

}








































